home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / TinyGL.lha / tinygl / src / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-25  |  4.0 KB  |  256 lines

  1. #include "zgl.h"
  2.  
  3. static char *op_table_str[]=
  4. {
  5. #define ADD_OP(a,b,c) "gl" #a " " #c,
  6.  
  7. #include "opinfo.h"
  8. };
  9.  
  10. static void (*op_table_func[])(GLContext *,GLParam *)=
  11. {
  12. #define ADD_OP(a,b,c) glop ## a ,
  13.  
  14. #include "opinfo.h"
  15. };
  16.  
  17. static int op_table_size[]=
  18. {
  19. #define ADD_OP(a,b,c) b + 1 ,
  20.  
  21. #include "opinfo.h"
  22. };
  23.  
  24.  
  25. GLContext *gl_get_context(void)
  26. {
  27.   return gl_ctx;
  28. }
  29.  
  30. static GLList *find_list(GLContext *c,unsigned int list)
  31. {
  32.   return c->shared_state.lists[list];
  33. }
  34.  
  35. static void delete_list(GLContext *c,int list)
  36. {
  37.   GLParamBuffer *pb,*pb1;
  38.   GLList *l;
  39.  
  40.   l=find_list(c,list);
  41.   assert(l != NULL);
  42.   
  43.   /* free param buffer */
  44.   pb=l->first_op_buffer;
  45.   while (pb!=NULL) {
  46.     pb1=pb->next;
  47.     gl_free(pb);
  48.     pb=pb1;
  49.   }
  50.   
  51.   gl_free(l);
  52.   c->shared_state.lists[list]=NULL;
  53. }
  54.  
  55. static GLList *alloc_list(GLContext *c,int list)
  56. {
  57.   GLList *l;
  58.   GLParamBuffer *ob;
  59.  
  60.   l=gl_zalloc(sizeof(GLList));
  61.   ob=gl_zalloc(sizeof(GLParamBuffer));
  62.  
  63.   ob->next=NULL;
  64.   l->first_op_buffer=ob;
  65.   
  66.   ob->ops[0].op=OP_EndList;
  67.  
  68.   c->shared_state.lists[list]=l;
  69.   return l;
  70. }
  71.  
  72.  
  73. void gl_print_op(FILE *f,GLParam *p)
  74. {
  75.   int op;
  76.   char *s;
  77.  
  78.   op=p[0].op;
  79.   p++;
  80.   s=op_table_str[op];
  81.   while (*s != 0) {
  82.     if (*s == '%') {
  83.       s++;
  84.       switch (*s++) {
  85.       case 'f':
  86.     fprintf(f,"%g",p[0].f);
  87.     break;
  88.       default:
  89.     fprintf(f,"%d",p[0].i);
  90.     break;
  91.       }
  92.       p++;
  93.     } else {
  94.       fputc(*s,f);
  95.       s++;
  96.     }
  97.   }
  98.   fprintf(f,"\n");
  99. }
  100.  
  101.  
  102. void gl_compile_op(GLContext *c,GLParam *p)
  103. {
  104.   int op,op_size;
  105.   GLParamBuffer *ob,*ob1;
  106.   int index,i;
  107.  
  108.   op=p[0].op;
  109.   op_size=op_table_size[op];
  110.   index=c->current_op_buffer_index;
  111.   ob=c->current_op_buffer;
  112.  
  113.   /* we should be able to add a NextBuffer opcode */
  114.   if ((index + op_size) > (OP_BUFFER_MAX_SIZE-2)) {
  115.  
  116.     ob1=gl_zalloc(sizeof(GLParamBuffer));
  117.     ob1->next=NULL;
  118.  
  119.     ob->next=ob1;
  120.     ob->ops[index].op=OP_NextBuffer;
  121.     ob->ops[index+1].p=(void *)ob1;
  122.  
  123.     c->current_op_buffer=ob1;
  124.     ob=ob1;
  125.     index=0;
  126.   }
  127.  
  128.   for(i=0;i<op_size;i++) {
  129.     ob->ops[index]=p[i];
  130.     index++;
  131.   }
  132.   c->current_op_buffer_index=index;
  133. }
  134.  
  135. void gl_add_op(GLParam *p)
  136. {
  137.   GLContext *c=gl_get_context();
  138.   int op;
  139.  
  140.   op=p[0].op;
  141.   if (c->exec_flag) {
  142.     op_table_func[op](c,p);
  143.   }
  144.   if (c->compile_flag) {
  145.     gl_compile_op(c,p);
  146.   }
  147.   if (c->print_flag) {
  148.     gl_print_op(stderr,p);
  149.   }
  150. }
  151.  
  152. /* this opcode is never called directly */
  153. void glopEndList(GLContext *c,GLParam *p)
  154. {
  155.   assert(0);
  156. }
  157.  
  158. /* this opcode is never called directly */
  159. void glopNextBuffer(GLContext *c,GLParam *p)
  160. {
  161.   assert(0);
  162. }
  163.  
  164.  
  165. void glopCallList(GLContext *c,GLParam *p)
  166. {
  167.   GLList *l;
  168.   int list,op;
  169.  
  170.   list=p[1].ui;
  171.   l=find_list(c,list);
  172.   if (l == NULL) gl_fatal_error("list %d not defined",list);
  173.   p=l->first_op_buffer->ops;
  174.  
  175.   while (1) {
  176.     op=p[0].op;
  177.     if (op == OP_EndList) break;
  178.     if (op == OP_NextBuffer) {
  179.       p=(GLParam *)p[1].p;
  180.     } else {
  181.       op_table_func[op](c,p);
  182.       p+=op_table_size[op];
  183.     }
  184.   }
  185. }
  186.  
  187.  
  188.  
  189. void glNewList(unsigned int list,int mode)
  190. {
  191.   GLList *l;
  192.   GLContext *c=gl_get_context();
  193.  
  194.   assert(mode == GL_COMPILE || mode == GL_COMPILE_AND_EXECUTE);
  195.   assert(c->compile_flag == 0);
  196.  
  197.   l=find_list(c,list);
  198.   if (l!=NULL) delete_list(c,list);
  199.   l=alloc_list(c,list);
  200.  
  201.   c->current_op_buffer=l->first_op_buffer;
  202.   c->current_op_buffer_index=0;
  203.   
  204.   c->compile_flag=1;
  205.   c->exec_flag=(mode == GL_COMPILE_AND_EXECUTE);
  206. }
  207.  
  208. void glEndList(void)
  209. {
  210.   GLContext *c=gl_get_context();
  211.   GLParam p[1];
  212.  
  213.   assert(c->compile_flag == 1);
  214.   
  215.   /* end of list */
  216.   p[0].op=OP_EndList;
  217.   gl_compile_op(c,p);
  218.   
  219.   c->compile_flag=0;
  220.   c->exec_flag=1;
  221. }
  222.  
  223. int glIsList(unsigned int list)
  224. {
  225.   GLContext *c=gl_get_context();
  226.   GLList *l;
  227.   l=find_list(c,list);
  228.   return (l != NULL);
  229. }
  230.  
  231. unsigned int glGenLists(int range)
  232. {
  233.   GLContext *c=gl_get_context();
  234.   int count,i,list;
  235.   GLList **lists;
  236.  
  237.   lists=c->shared_state.lists;
  238.   count=0;
  239.   for(i=0;i<MAX_DISPLAY_LISTS;i++) {
  240.     if (lists[i]==NULL) {
  241.       count++;
  242.       if (count == range) {
  243.                 list=i-range+1;
  244.                 for(i=0;i<range;i++) {
  245.                     alloc_list(c,list+i);
  246.                 }
  247.                 return (unsigned int)list;
  248.       }
  249.     } else {
  250.       count=0;
  251.     }
  252.   }
  253.   return 0;
  254. }
  255.  
  256.